package de.velvettear.musiclister.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import de.velvettear.musiclister.util.SettingsHelper;

public class Settings extends JDialog implements ActionListener {

	private final static String ACTION_CANCEL = "ACTION_CANCEL";
	private final static String ACTION_SAVE = "ACTION_SAVE";
	
	private final JPanel contentPanel = new JPanel();
	private JTextField txt_host;
	private JTextField txt_user;
	private JTextField txt_password;
	private JTextField txt_prefix;
	private JTextField txt_database;

	public static void main(String[] args) {
		try {
			Settings dialog = new Settings();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Settings() {
		setTitle("Settings");
		setBounds(100, 100, 450, 300);
		setLocationRelativeTo(null);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		GridBagLayout gbl_contentPanel = new GridBagLayout();
		gbl_contentPanel.columnWidths = new int[]{0, 0};
		gbl_contentPanel.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
		gbl_contentPanel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gbl_contentPanel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		contentPanel.setLayout(gbl_contentPanel);
		{
			JLabel lbl_host = new JLabel("Host");
			lbl_host.setFont(new Font("Tahoma", Font.BOLD, 14));
			GridBagConstraints gbc_lbl_host = new GridBagConstraints();
			gbc_lbl_host.anchor = GridBagConstraints.WEST;
			gbc_lbl_host.insets = new Insets(10, 5, 5, 5);
			gbc_lbl_host.gridx = 0;
			gbc_lbl_host.gridy = 0;
			contentPanel.add(lbl_host, gbc_lbl_host);
		}
		{
			txt_host = new JTextField();
			GridBagConstraints gbc_txt_host = new GridBagConstraints();
			gbc_txt_host.insets = new Insets(0, 5, 5, 5);
			gbc_txt_host.fill = GridBagConstraints.HORIZONTAL;
			gbc_txt_host.gridx = 0;
			gbc_txt_host.gridy = 1;
			contentPanel.add(txt_host, gbc_txt_host);
			txt_host.setColumns(10);
		}
		{
			JLabel lbl_user = new JLabel("User");
			lbl_user.setFont(new Font("Tahoma", Font.BOLD, 14));
			GridBagConstraints gbc_lbl_user = new GridBagConstraints();
			gbc_lbl_user.anchor = GridBagConstraints.WEST;
			gbc_lbl_user.insets = new Insets(0, 5, 5, 5);
			gbc_lbl_user.gridx = 0;
			gbc_lbl_user.gridy = 2;
			contentPanel.add(lbl_user, gbc_lbl_user);
		}
		{
			txt_user = new JTextField();
			GridBagConstraints gbc_txt_user = new GridBagConstraints();
			gbc_txt_user.insets = new Insets(0, 5, 5, 5);
			gbc_txt_user.fill = GridBagConstraints.HORIZONTAL;
			gbc_txt_user.gridx = 0;
			gbc_txt_user.gridy = 3;
			contentPanel.add(txt_user, gbc_txt_user);
			txt_user.setColumns(10);
		}
		{
			JLabel lbl_password = new JLabel("Password");
			lbl_password.setFont(new Font("Tahoma", Font.BOLD, 14));
			GridBagConstraints gbc_lbl_password = new GridBagConstraints();
			gbc_lbl_password.anchor = GridBagConstraints.WEST;
			gbc_lbl_password.insets = new Insets(0, 5, 5, 5);
			gbc_lbl_password.gridx = 0;
			gbc_lbl_password.gridy = 4;
			contentPanel.add(lbl_password, gbc_lbl_password);
		}
		{
			txt_password = new JTextField();
			GridBagConstraints gbc_txt_password = new GridBagConstraints();
			gbc_txt_password.insets = new Insets(0, 5, 5, 5);
			gbc_txt_password.fill = GridBagConstraints.HORIZONTAL;
			gbc_txt_password.gridx = 0;
			gbc_txt_password.gridy = 5;
			contentPanel.add(txt_password, gbc_txt_password);
			txt_password.setColumns(10);
		}
		{
			JLabel lbl_database = new JLabel("Database");
			lbl_database.setFont(new Font("Tahoma", Font.BOLD, 14));
			GridBagConstraints gbc_lbl_database = new GridBagConstraints();
			gbc_lbl_database.anchor = GridBagConstraints.WEST;
			gbc_lbl_database.insets = new Insets(0, 5, 5, 5);
			gbc_lbl_database.gridx = 0;
			gbc_lbl_database.gridy = 6;
			contentPanel.add(lbl_database, gbc_lbl_database);
		}
		{
			txt_database = new JTextField();
			GridBagConstraints gbc_txt_database = new GridBagConstraints();
			gbc_txt_database.insets = new Insets(0, 5, 5, 5);
			gbc_txt_database.fill = GridBagConstraints.HORIZONTAL;
			gbc_txt_database.gridx = 0;
			gbc_txt_database.gridy = 7;
			contentPanel.add(txt_database, gbc_txt_database);
			txt_database.setColumns(10);
		}
		{
			JLabel lbl_prefix = new JLabel("Prefix");
			lbl_prefix.setFont(new Font("Tahoma", Font.BOLD, 14));
			GridBagConstraints gbc_lbl_prefix = new GridBagConstraints();
			gbc_lbl_prefix.anchor = GridBagConstraints.WEST;
			gbc_lbl_prefix.insets = new Insets(0, 5, 10, 5);
			gbc_lbl_prefix.gridx = 0;
			gbc_lbl_prefix.gridy = 8;
			contentPanel.add(lbl_prefix, gbc_lbl_prefix);
		}
		{
			txt_prefix = new JTextField();
			GridBagConstraints gbc_txt_prefix = new GridBagConstraints();
			gbc_txt_prefix.insets = new Insets(0, 5, 10, 5);
			gbc_txt_prefix.fill = GridBagConstraints.HORIZONTAL;
			gbc_txt_prefix.gridx = 0;
			gbc_txt_prefix.gridy = 9;
			contentPanel.add(txt_prefix, gbc_txt_prefix);
			txt_prefix.setColumns(10);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton btn_save = new JButton("Save");
				btn_save.addActionListener(this);
				btn_save.setActionCommand(ACTION_SAVE);
				buttonPane.add(btn_save);
				getRootPane().setDefaultButton(btn_save);
			}
			{
				JButton btn_cancel = new JButton("Cancel");
				btn_cancel.addActionListener(this);
				btn_cancel.setActionCommand(ACTION_CANCEL);
				buttonPane.add(btn_cancel);
			}
		}
		
		loadSettings();
		
		pack();		
	}

	private void loadSettings() {
		HashMap<String, String> settings = SettingsHelper.readSettings();
		
		String host = settings.get(SettingsHelper.HOST).toString();
		String user = settings.get(SettingsHelper.USER).toString();
		String password = settings.get(SettingsHelper.PASSWORD).toString();
		String database = settings.get(SettingsHelper.DATABASE).toString();	
		String prefix = settings.get(SettingsHelper.PREFIX).toString();
		
		txt_host.setText(host);
		txt_user.setText(user);
		txt_password.setText(password);
		txt_database.setText(database);
		txt_prefix.setText(prefix);		
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals(ACTION_SAVE)) {
			SettingsHelper.writeSettings(txt_host.getText(), txt_user.getText(), txt_password.getText(), txt_database.getText(), txt_prefix.getText());
			dispose();
		} else if (e.getActionCommand().equals(ACTION_CANCEL)) {
			dispose();
		}		
	}

}
